package com.webchat.client.payment.controller;


import com.webchat.client.payment.service.PaymentOrderService;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.dto.payment.PaymentOrderDTO;
import com.webchat.domain.dto.payment.PaymentResultDTO;
import com.webchat.domain.dto.payment.PaymentTransferRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/payment-service/order")
public class PaymentOrderController {


    @Autowired
    private PaymentOrderService paymentOrderService;

    /**
     * 获取订单详情
     *
     * @param orderId
     * @param accessToken
     * @return
     */
    @GetMapping("/info/{orderId}")
    public APIResponseBean<PaymentOrderDTO> orderInfo(@PathVariable String orderId,
                                                      @RequestHeader(name = "access-token") String accessToken) {

        String userId = SessionHelper.getCurrentUserId();
        PaymentOrderDTO order = paymentOrderService.orderInfo(orderId, userId, accessToken);
        return APIResponseBeanUtil.success(order);
    }


    /**
     * 确认支付
     *
     * @param paymentTransferRequest
     * @param accessToken 交易凭证
     * @return
     */
    @PostMapping("/transfer")
    public APIResponseBean<PaymentResultDTO> transfer(@RequestBody PaymentTransferRequestDTO paymentTransferRequest,
                                             @RequestHeader(name = "access-token") String accessToken) {

        String userId = SessionHelper.getCurrentUserId();
        paymentTransferRequest.setUserId(userId);
        PaymentResultDTO paymentRes = paymentOrderService.transfer(paymentTransferRequest, accessToken);
        return APIResponseBeanUtil.success(paymentRes);
    }

}
